/*
 * Copyright 2007-2010 Jeremy Brown.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author <a href="http://jeremy-brown.com">Jeremy Brown</a>
 */


package groovy.prefuse.factory

import prefuse.render.*
import prefuse.Visualization

public class RendererFactory extends AbstractFactory
{
	private Class klass

	public RendererFactory(Class klass)
	{
		this.klass = klass
	}
	
	boolean isLeaf()
	{
		return true
	}
    
    public Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException
	{
		def forNode = attributes.remove("forNode")
		def forEdge = attributes.remove("forEdge")
		builder.context.put("predicate", attributes.remove("predicate"))

		//Shortcut properties align, align1, align2, valign, valign1, valign2 replaced with horizontalAlignment, horizontalAlignment1, horizontalAlignment2, verticalAlignment, verticalAlignment1, verticalAlignment2
		if(attributes.align != null)
		{
			attributes.horizontalAlignment = attributes.remove("align")
		}
		if(attributes.align1 != null)
		{
			attributes.horizontalAlignment1 = attributes.remove("align1")
		}
		if(attributes.align2 != null)
		{
			attributes.horizontalAlignment2 = attributes.remove("align2")
		}
		if(attributes.alignImage != null)
		{
			attributes.horizontalImageAlignment = attributes.remove("alignImage")
		}
		if(attributes.alignText != null)
		{
			attributes.horizontalTextAlignment = attributes.remove("alignText")
		}
		if(attributes.hpadding != null)
		{
			attributes.horizontalPadding = attributes.remove("hpadding")
		}
		if(attributes.valign != null)
		{
			attributes.verticalAlignment = attributes.remove("valign")
		}
		if(attributes.valign1 != null)
		{
			attributes.verticalAlignment1 = attributes.remove("valign1")
		}
		if(attributes.valign2 != null)
		{
			attributes.verticalAlignment2 = attributes.remove("valign2")
		}
		if(attributes.valignImage != null)
		{
			attributes.verticalImageAlignment = attributes.remove("valignImage")
		}
		if(attributes.valignText != null)
		{
			attributes.verticalTextAlignment = attributes.remove("valignText")
		}
		if(attributes.vpadding != null)
		{
			attributes.verticalPadding = attributes.remove("vpadding")
		}

		if(value == null)
		{
			if(forNode instanceof Renderer || (forNode instanceof Class && Renderer.isAssignableFrom(value)))
			{
				value = forNode
				forNode = true
				forEdge = false
			}
			else if(forEdge instanceof Renderer || (forEdge instanceof Class && Renderer.isAssignableFrom(value)))
			{
				value = forEdge
				forEdge = true
				forNode = false
			}
			else if(attributes.render)
			{
				value = attributes.remove("render")
			}
		}
				
		if(!builder.context.predicate && !forNode && !forEdge)
		{
			forNode = true
		}
		
		builder.context.put("forNode", forNode)
		builder.context.put("forEdge", forEdge)
		
		if(value instanceof Renderer)
		{
			return value
		}

		def buildClass
		if(klass)
		{
			buildClass = klass
		}
		else if(value instanceof Class)
		{
			buildClass = value
			value = null
		}
		
		def renderer = null
		
		if(buildClass instanceof Class && Renderer.isAssignableFrom(buildClass))
		{
			renderer = buildClass.newInstance()
		}
		
		return renderer
	}

	
    public void setParent(FactoryBuilderSupport builder, Object parent, Object child) 
	{
		if(!(parent instanceof Visualization))
		{
			return
		}
		
		if(parent.getRendererFactory() == null)
		{
			parent.setRendererFactory(new DefaultRendererFactory())
		}
		
		if(builder.context.forNode)
		{
			parent.getRendererFactory().setDefaultRenderer(child)
		}
		else if(builder.context.forEdge)
		{
			parent.getRendererFactory().setDefaultEdgeRenderer(child)
		}
		else if(builder.context.predicate)
		{
			parent.getRendererFactory().add(builder.context.predicate, child)
		}
    }
}
